require 'scanner/states/state'
require 'scanner/states/start_state'

class CommentBlockState < State
  @token_type = :CommentBlock
  @counter = 1
  @prev = nil

  def self.on_enter(token, char)
    literal = ""
    literal = token.literal unless token.nil?
    literal << char.to_s
    # Remove leading slashes
    if literal[0..1] == "/*"
      literal = literal[2..-1]
    end
    @token = Token.new(@token_type, "#{literal}")
  end

  def self.next_state(char)
    if @counter <= 0
      @counter = 1
      # Chop off the final "*/"
      @token.literal = @token.literal[0..-3]
      @prev = nil
      return StartState
    else
      if char.nil? or (char == "/" and @prev == "*")
        @counter = @counter - 1
        @prev = nil
        # puts "-#{@counter}: #{@token.literal.inspect}"
      elsif char == "*" and @prev == "/"
        @counter = @counter + 1
        @prev = nil
        # puts "+#{@counter}: #{@token.literal.inspect}"
      else
        @prev = char
      end
      return CommentBlockState
    end
  end
end